package jcu.ict.profaid;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class PhoneDBAdapter {
	public static final String KEY_ID = "phone_id";
	public static final String KEY_USER = "user_id";
	public static final String KEY_QUESTION_ID = "question_id";
	public static final String KEY_COURSE_ID = "CourseId";
	public static final String KEY_PHONE_NUMBER = "PhoneNumber";
	private static final String DATABASE_TABLE = "PhoneNumber";

	private Context context;

	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;

	public PhoneDBAdapter(Context ctx) {
		this.context = ctx;
	}

	// --- open the database ---
	public PhoneDBAdapter open() throws SQLException {
		DBHelper = new DatabaseHelper(context);
		db = DBHelper.getWritableDatabase();

		return this;
	}

	// ---closes the database ---
	public void close() {
		DBHelper.close();
	}

	// --- insert Question to question table ---
	public long insertAnswerList(String user_id, String phonenumber, String question_id, String course_id) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_USER, user_id);
		initialValues.put(KEY_PHONE_NUMBER, phonenumber);
		initialValues.put(KEY_QUESTION_ID, question_id);
		initialValues.put(KEY_COURSE_ID, course_id);
		return db.insert(DATABASE_TABLE, null, initialValues);
	}

	// --- populate Question from database ---
	public Cursor populateNumber(String id_course, String id_question) throws SQLException {
		Cursor mCursor =
				db.query(true, DATABASE_TABLE, new String[] {KEY_PHONE_NUMBER}, KEY_QUESTION_ID + "=\"" + id_question +  "\"" + " AND "+ KEY_COURSE_ID + "=\"" + id_course +  "\"", null,
						null, null, null, null);
	
		return mCursor;
	}
	
	public Cursor searchNumber(String number) throws SQLException {
		Cursor mCursor = db.query(true, DATABASE_TABLE,
				new String[] { KEY_ID }, KEY_PHONE_NUMBER + "=\"" + number
						+ "\"", null, null, null, null, null);
		return mCursor;

	}
	
}
